<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=0">
    <link rel="stylesheet" href="/static/layuiadmin/layui/css/layui.css" media="all">
</head>
<style>
    input:disabled {
        background: #e8e8e8;
    }
</style>
<body>
<div class="layui-form" lay-filter="layuiadmin-app-form-list" id="layuiadmin-app-form-list" style="padding: 20px 30px 0 0;">
    <input type="hidden" name="id" value="{$find.id}">
    <input type="hidden" name="gid" value="{$find.gid}">
    <div class="layui-form-item">
        <label class="layui-form-label">大礼包名称</label>
        <div class="layui-input-inline">
            <input type="text" name="name" value="{$find.name}" placeholder="大礼包名称" autocomplete="off" class="layui-input">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">日期范围</label>
        <div class="layui-input-inline">
            <input type="text" name="date" class="layui-input" id="test" lay-verify="required" placeholder=" - " autocomplete="off" {if condition="$find.start neq '' && $find.end neq ''"}value="{$find.start|date='Y-m-d', ###} - {$find.end|date='Y-m-d', ###}"{/if}>
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">大礼包封面</label>
        <div class="layui-input-inline">
            <input type="text" name="img" value="{$find.img}" lay-verify="required" placeholder="请上传大礼包封面" autocomplete="off" class="layui-input">
        </div>
        <button style="float: left;" type="button" class="layui-btn" id="up">上传封面</button>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">礼品选择</label>
        <div class="layui-input-inline">
            <input type="text" class="layui-input" value="{$find.lipin}" ts-selected="{$find.gid}" id="demo" lay-verify="required" placeholder="请选择礼品" autocomplete="off">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">价格</label>
        <div class="layui-input-inline">
            <input type="number" name="money" value="{$find.money ? $find.money : 0}" class="layui-input" lay-verify="required|number" placeholder="支付金额" autocomplete="off">
        </div>
        <div style="height: 38px;line-height: 38px;">0为免费</div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">排序</label>
        <div class="layui-input-inline">
            <input type="number" name="sort" value="{$find.sort}" class="layui-input" lay-verify="required|number" placeholder="排序" autocomplete="off">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">领取状态</label>
        <div class="layui-input-inline" style="width: 400px;">
            <input type="radio" name="type" value="1" title="开启领取" {if condition="$find.type == '1'"}checked="checked"{elseif condition="!$find"/}checked="checked"{/if}>
            <input type="radio" name="type" value="0" title="关闭领取" {if condition="$find.type == '0'"}checked="checked"{/if}>
        </div>
    </div>
    <div class="layui-form-item layui-hide">
        <input type="button" lay-submit lay-filter="layuiadmin-app-form-submit" id="layuiadmin-app-form-submit" value="审核">
    </div>
</div>
<script src="/static/layuiadmin/layui/layui.js"></script>
<script>
    layui.config({
        base: '/static/layuiadmin/' //静态资源所在路径
    }).extend({
        index: 'lib/index' //主入口模块
    }).use(['index', 'laydate', 'form', 'tableSelect', 'upload'], function(){
        var $ = layui.$,
                form = layui.form,
                laydate = layui.laydate,
                upload = layui.upload,
                tableSelect = layui.tableSelect;

        //普通图片上传
        upload.render({
            elem: '#up'
            ,url: "{:url('Gift/upload')}"
            ,done: function(res){
                //如果上传失败
                if(res.code > 0){
                    return layer.msg('上传失败');
                }
                //上传成功
                layer.msg('上传成功');
                $("input[name=img]").val(res.img);
            }
        });

        tableSelect.render({
            elem: '#demo',	//定义输入框input对象
            checkedKey: 'id', //表格的唯一建值，非常重要，影响到选中状态 必填
            searchKey: 'lipin',	//搜索输入框的name值 默认keyword
            searchPlaceholder: '搜索礼品名称',	//搜索输入框的提示文字 默认关键词搜索
            table: {	//定义表格参数，与LAYUI的TABLE模块一致，只是无需再定义表格elem
                url: "{:url('Gift/index', ['is_zu' => 1])}",
                method: 'post',
                cols: [[
                    {type: 'checkbox'},
                    {field: 'lipin', title: '礼品名称', width: 200},
                    {field: 'uid', title: '提供礼品商家', width: 200},
                    {field: 'num', title: '礼品数量', width: 100},
                    {field: 'stage', title: '期数', width: 100},
                    {field: 'mode', title: '领取方法', width: 150, templet: "#mode"},
                    {field: 'money', title: '任务金额', width: 150, templet: "#money"}
                ]]
            },
            done: function (elem, data) {
                //选择完后的回调，包含2个返回值 elem:返回之前input对象；data:表格返回的选中的数据 []
                //拿到data[]后 就按照业务需求做想做的事情啦~比如加个隐藏域放ID...
                var field = data.data;
                var id = [];
                var name = [];
                for (var i=0;i<field.length;i++){
                    id.push(field[i].id);
                    name.push(field[i].lipin);
                }
                id.join(',');
                name.join(',');
                $("input[name=gid]").val(id);
                $("#demo").val(name);
            }
        });

        //日期范围
        laydate.render({
            elem: '#test'
            ,range: true
        });

        //监听提交
        form.on('submit(layuiadmin-app-form-submit)', function(data){
            var field = data.field; //获取提交的字段
            var index = parent.layer.getFrameIndex(window.name); //先得到当前iframe层的索引
            //提交 Ajax 成功后，关闭当前弹层并重载表格
            var load = layer.load();
            $.ajax({
                type: "POST",
                url: "{:url('Gift/package_examine_shenhe')}",
                data: {field:field},
                dataType: "json",
                success: function(e){
                    layer.close(load);
                    if (e.info == 1){
                        layer.msg('审核成功', {offset: ['30%', '40%']});
                        setTimeout(function () {
                            parent.layui.table.reload('demo'); //重载表格
                            parent.layer.close(index); //再执行关闭
                        }, 500)
                    }else{
                        layer.msg(e.msg, {offset: ['30%', '40%']});
                    }
                }
            });
        });
    })
</script>
</body>
</html>